package com.sanyang.pda.presenter;


import com.google.gson.Gson;
import com.sanyang.pda.bean.ByBarcodeBean;
import com.sanyang.pda.bean.GoodsDetailBean;
import com.sanyang.pda.bean.request.PalletSubmit;
import com.sanyang.pda.contract.CheckContract;
import com.sanyang.pda.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class CheckPresenter extends BasePresenter<CheckContract.View> implements CheckContract.Presenter {

    @Override
    public void inboundDetail(String entryNo,String barcode,String pallet) {
        NetUtils.geApi().inboundDetail(entryNo, barcode, pallet)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<GoodsDetailBean>>() {
                    @Override
                    public void accept(BaseAppEntity<GoodsDetailBean> listBaseRowsAppEntity) throws Exception {
                        //getView().showMessage("请求成功"+listBaseRowsAppEntity.getMsg());
                        getView().setData(listBaseRowsAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //getView().showMessage("请求失败"+throwable.getMessage());
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    /*@Override
    public void qualifyVerify(String pallet, String articleId) {
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("pallet",pallet);
            jsonObject.put("articleId",articleId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), jsonObject.toString());
        NetUtils.geApi().qualifyVerify(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<VerifyPalletBean>>() {
                    @Override
                    public void accept(BaseAppEntity<VerifyPalletBean> goodsDetailBeanBaseAppEntity) throws Exception {
                        getView().setPallet(goodsDetailBeanBaseAppEntity.getData(),pallet);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }*/

    @Override
    public void inventorySubmit(PalletSubmit palletSubmit) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(palletSubmit));
        NetUtils.geApi().inventorySubmit(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<GoodsDetailBean>>() {
                    @Override
                    public void accept(BaseAppEntity<GoodsDetailBean> goodsDetailBeanBaseAppEntity) throws Exception {
                        getView().palletSuccess();
                        getView().showMessage(goodsDetailBeanBaseAppEntity.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void palletShelves(PalletSubmit palletSubmit) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(palletSubmit));
        NetUtils.geApi().palletShelves(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<GoodsDetailBean>>() {
                    @Override
                    public void accept(BaseAppEntity<GoodsDetailBean> goodsDetailBeanBaseAppEntity) throws Exception {
                        getView().palletSuccess();
                        getView().showMessage(goodsDetailBeanBaseAppEntity.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    /* @Override
     public void selectArticleInfo(String entryNo, String articleId,String code) {
         NetUtils.geApi().selectArticleInfo(entryNo, articleId)
                 .compose(this.handleEverythingResult())
                 .subscribe(new Consumer<BaseAppEntity<ArticleInfoBean>>() {
                     @Override
                     public void accept(BaseAppEntity<ArticleInfoBean> articleInfoBeanBaseAppEntity) throws Exception {
                         getView().setArticleInfoBean(articleInfoBeanBaseAppEntity.getData(),code);
                     }
                 }, new Consumer<Throwable>() {
                     @Override
                     public void accept(Throwable throwable) throws Exception {
                         getView().showMessage(throwable.getMessage());
                     }
                 });
     }*/
    @Override
    public void selectArticleInfoByBarcode(String barcode,String number) {
        NetUtils.geApi().selectArticleInfoByBarCodeAndNo(barcode,number)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<ByBarcodeBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<ByBarcodeBean>> listBaseAppEntity) throws Exception {
                        getView().setGoodsList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
